package com.cn.ccp.action.faculty;

import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.TeachingGroupBean;

public class TeachingGroupAction extends BaseAction{

	private List<TeachingGroupBean> teachingGroupList ;
	private TeachingGroupBean teachingGroupBean ;
	
	
	public String goUpdate(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		teachingGroupBean = getTeachingGroupService().showBean(Integer.parseInt(id));
		return UPDATE;
	}
	
	public String edit(){
		if(teachingGroupBean.getFaculity_id() == null || teachingGroupBean.getFaculity_id().equals("-1")){
			showMsg("facultyId" , "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		if(teachingGroupBean.getTeaching_group_name() == null || teachingGroupBean.getTeaching_group_name().equals("")){
			showMsg("name" , "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		if(teachingGroupBean.getId() != 0 )
			getTeachingGroupService().update(teachingGroupBean);
		else
			getTeachingGroupService().insert(teachingGroupBean);
		getpagedetail();
		return MAIN;
	}
	
	
	public void doDelete(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		if(getTeachingGroupService().delete(Integer.parseInt(id)))
			getpagedetail();
	}
	
	
	@Override
	public void getpagedetail() {
		setItemCount(getTeachingGroupService().getCount());
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		teachingGroupList = getTeachingGroupService().getTeachingGroupList(start, pagesize);
	}
	
	
	public List<TeachingGroupBean> getTeachingGroupList() {
		if(teachingGroupList == null)
			getpagedetail();
		return teachingGroupList;
	}
	public void setTeachingGroupList(List<TeachingGroupBean> teachingGroupList) {
		this.teachingGroupList = teachingGroupList;
	}
	public TeachingGroupBean getTeachingGroupBean() {
		if(teachingGroupBean == null)
			teachingGroupBean = new TeachingGroupBean();
		return teachingGroupBean;
	}
	public void setTeachingGroupBean(TeachingGroupBean teachingGroupBean) {
		this.teachingGroupBean = teachingGroupBean;
	}
	
}
